package org.com.blbl.VideoShare;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class VideoShareMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
    private LongWritable shareCount = new LongWritable();
    private Text title = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // Split the line by the delimiter (",")
        String[] fields = value.toString().split(",");
        if (fields.length >= 14) {
            try {
                String titleField = fields[0].trim(); // 标题
                long shareCountField = Long.parseLong(fields[13].trim()); // 分享量

                title.set(titleField);
                shareCount.set(shareCountField);

                context.write(shareCount, title);
            } catch (NumberFormatException e) {
            }
        }
    }
}
